package com.android.rundemo.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.android.rundemo.R;
import com.android.rundemo.utils.DataUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import cn.qqtheme.framework.picker.NumberPicker;
import cn.qqtheme.framework.picker.OptionPicker;

public class firstUseActivity extends Activity {

    private static final String TAG = "firstUseActivity";

    //个人信息文件名
    String fileName = "personalData";

    TextView et_name, et_age, et_height, et_weight;

    Button btn_infoConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_use);

        checkPermission();

        //确认按钮
        btn_infoConfirm = findViewById(R.id.btn_infoConfirm);
        btn_infoConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_name.getText().toString();
                String age = et_age.getText().toString();
                String height = et_height.getText().toString();
                String weight = et_weight.getText().toString();

                if (name.length() == 0 || age.length() == 0 || height.length() == 0 || weight.length() == 0) {
                    Toast.makeText(firstUseActivity.this, "数据不能为空!", Toast.LENGTH_SHORT).show();
                } else if (name.length() > 8) {
                    Toast.makeText(firstUseActivity.this, "昵称不能超过8个字节!", Toast.LENGTH_SHORT).show();
                } else if (!DataUtil.isNumeric(age) || !DataUtil.isNumeric(height) || !DataUtil.isNumeric(weight)
                        || Integer.parseInt(age) > 100 || Integer.parseInt(age) < 0
                        || Integer.parseInt(height) > 300 || Integer.parseInt(height) < 0
                        || Integer.parseInt(weight) > 200 || Integer.parseInt(weight) < 0) {
                    Toast.makeText(firstUseActivity.this, "年龄、身高、体重输入有误!", Toast.LENGTH_SHORT).show();
                } else {
                    finish();
                }
            }
        });

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);

        et_age.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NumberPicker picker = new NumberPicker(firstUseActivity.this);
                picker.setOffset(2);//偏移量
                picker.setRange(10, 100);//数字范围
                picker.setSelectedItem(20);
                picker.setLabel("岁");
                picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                    @Override
                    public void onOptionPicked(String option) {
                        et_age.setText(option);
                    }
                });
                picker.show();
            }
        });

        et_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NumberPicker picker = new NumberPicker(firstUseActivity.this);
                picker.setOffset(2);//偏移量
                picker.setRange(145, 200);//数字范围
                picker.setSelectedItem(170);
                picker.setLabel("厘米");
                picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                    @Override
                    public void onOptionPicked(String option) {
                        et_height.setText(option);
                    }
                });
                picker.show();
            }
        });

        et_weight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NumberPicker picker = new NumberPicker(firstUseActivity.this);
                picker.setOffset(2);//偏移量
                picker.setRange(40, 200);//数字范围
                picker.setSelectedItem(60);
                picker.setLabel("公斤");
                picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                    @Override
                    public void onOptionPicked(String option) {
                        et_weight.setText(option);
                    }
                });
                picker.show();
            }
        });
    }

    /**
     * 存储个人信息至本地
     */
    private void savePersonalData() {
        OutputStream outputStream = null;
        try {
            outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            String name = et_name.getText().toString() + "\n";
            String age = et_age.getText().toString() + "\n";
            String height = et_height.getText().toString() + "\n";
            String weight = et_weight.getText().toString() + "\n";

            bufferedWriter.write(name);
            bufferedWriter.write(age);
            bufferedWriter.write(height);
            bufferedWriter.write(weight);
            bufferedWriter.close();

            //标记非第一次使用
            String isFirstUse = "false";
            OutputStream firstUseOS = openFileOutput("config", Context.MODE_PRIVATE);

            //这个地方复制粘贴忘记改导致找了三个小时的BUG，纪念一下 2019年05月30日18:50:18
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(firstUseOS));
            bw.write(isFirstUse);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //退出时存储数据
        savePersonalData();
    }

    private void checkPermission() {
        //所有的权限列表
        List<String> permissions = new ArrayList<>();
        permissions.add(Manifest.permission.INTERNET);
        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.ACCESS_NETWORK_STATE);
        permissions.add(Manifest.permission.ACCESS_WIFI_STATE);
        permissions.add(Manifest.permission.READ_PHONE_STATE);
        permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
        permissions.add(Manifest.permission.CHANGE_WIFI_STATE);
        permissions.add(Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS);
        permissions.add(Manifest.permission.BLUETOOTH);
        permissions.add(Manifest.permission.BLUETOOTH_ADMIN);

        //未获取的权限列表
        List<String> notGrantedList = new ArrayList<>();
        for (int i = 0; i < permissions.size(); i++) {
            String permission = permissions.get(i);
            int ret = ContextCompat.checkSelfPermission(this, permission);
            if (ret != PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "checkPermission::not granted permission: " + permission);
                notGrantedList.add(permission);
            }
        }

        //申请未获取的权限
        int notGrantedCount = notGrantedList.size();
        if (notGrantedCount > 0) {
            String[] permissionArr = notGrantedList.toArray(new String[notGrantedCount]);
            ActivityCompat.requestPermissions(this, permissionArr, 0);
        }
    }
}
